/**
 * 封装分页组件
 */
'use strict';
import React, {
    Component
} from 'react'
import {
    Link,
    hashHistory
} from 'react-router'

import {
    Pagination
} from 'react-bootstrap'

export default class bootstrapPage extends Component {
    constructor(props) {
        super(props);
        this.activepage=1
    }
    render() {
        let { nextPage, prePage, pageNum, pages ,itemclick} = this.props;
        let items = [];

            for(let i = 1;i <= pages ;i++) {
                let Pager=i==pageNum?<Pagination.Item active onClick={itemclick.bind(this,i)}>{i}</Pagination.Item>:<Pagination.Item  onClick={itemclick.bind(this,i)}>{i}</Pagination.Item>
                if (i<= 5) {
                    items.push(Pager)
                } else if(pages-4<i<pages) {
                    items.push(Pager)
                }
            }
            if(pages>10){
                let pager1=<Pagination.Item active onClick={itemclick.bind(this,pageNum)}>{pageNum}</Pagination.Item>;
                let pager2=<Pagination.Ellipsis disabled />
                if(5<pageNum && pageNum<pages-4){
                    if(pageNum==6){
                        items[5]=pager1;
                        items[6]=pager2;
                    }else if(pageNum<pages-5&&pageNum!=6){
                        items[5]=pager2;
                        items[6]=pager1
                    }else{
                        items[5]=pager2
                        items[6]=pager1
                        items[7]=pager2
                    }

                }else{
                    items[5]=(<Pagination.Ellipsis disabled />);
                }

            }

        return (
            <Pagination style={{ float:'right'}}>
                {
                    prePage==0?
                        [
                            <Pagination.First onClick={itemclick.bind(this,1)} />,
                            <Pagination.Prev disabled />
                        ]
                        :
                        [
                            <Pagination.First onClick={itemclick.bind(this,1)} />,
                            <Pagination.Prev  onClick={itemclick.bind(this,pageNum-1)}/>
                        ]
                }
                {items}
                {
                    nextPage==0?
                        [
                            <Pagination.Next disabled />,
                            <Pagination.Last   onClick={itemclick.bind(this,pages)}/>
                        ]
                        :
                        [
                            <Pagination.Next onClick={itemclick.bind(this,pageNum+1)} />,
                            <Pagination.Last onClick={itemclick.bind(this,pages)} />
                        ]
                }

            </Pagination>
        )
    }
}
